package com.lidong.lowcode.database.utils;

import com.lidong.lowcode.database.constants.BaseConst;
import com.lidong.lowcode.database.constants.database.SqlColumnEnum;
import com.lidong.lowcode.database.constants.enumconst.StrCaseEnum;
import com.lidong.lowcode.database.entity.database.DataBaseColumn;
import com.sun.istack.internal.NotNull;
import org.springframework.util.ObjectUtils;

import java.util.Locale;
import java.util.Objects;

/**
 * @author LiDong
 * @version 1.0.0
 * @createTime 12/7/2022 9:18 PM
 */
public enum CreateTableUtils {
    ;

    /**
     * 转换字段名称
     *
     * @param dataBaseColumn dataBaseColumn
     * @return String
     */
    public static String getColumnName(@NotNull DataBaseColumn dataBaseColumn) {
        StrCaseEnum nameCase = dataBaseColumn.getNameCase();
        String name = dataBaseColumn.getName();
        if (StrCaseEnum.UPPER == nameCase) {
            return name.toUpperCase(Locale.ROOT);
        } else {
            return name.toLowerCase(Locale.ROOT);
        }
    }

    /**
     * 获取字段的类型和字段的长度
     *
     * @param dataType       字段类型
     * @param dataTypeLength 字段长度
     * @param decimalPoint   字段精度
     * @return String
     */
    public static String getColumnTypeSql(SqlColumnEnum dataType, Integer dataTypeLength, Integer decimalPoint) {
        // 字段类型 sql
        String columnTypeSql = "";
        switch (dataType) {
            case INT:
                columnTypeSql = SqlColumnEnum.INT.name();
                break;
            case DATETIME:
                columnTypeSql = SqlColumnEnum.DATETIME.name();
                break;
            case BIGINT:
                columnTypeSql = SqlColumnEnum.BIGINT.name();
                break;
            default:
                columnTypeSql = SqlColumnEnum.VARCHAR.name();
        }
        // 字段长度 sql
        String lengthPointSql = "";
        if (dataType == SqlColumnEnum.DECIMAL) {
            lengthPointSql = dataTypeLength + BaseConst.COMMA + decimalPoint;
        } else {
            if (SqlColumnEnum.VARCHAR == dataType) {
                lengthPointSql = "10";
            } else {
                lengthPointSql = "";
            }
        }
        if (!ObjectUtils.isEmpty(lengthPointSql)) {
            columnTypeSql += BaseConst.LEFT_BRACKET + lengthPointSql + BaseConst.RIGHT_BRACKET;

        }
        return columnTypeSql;
    }

    /**
     * 拼接可空、默认值、字段备注
     *
     * @param notNull      是否可空
     * @param dataType     字段类型
     * @param defaultValue 默认值
     * @param remark       备注
     * @return String
     */
    public static String getOtherSql(boolean notNull, SqlColumnEnum dataType, boolean autoIncrement, boolean isPrimaryKey, Object defaultValue, String remark) {
        String sql = "";
        if (notNull) {
            sql += BaseConst.BLANK + "NOT NULL";
        }
        if (notNull && isPrimaryKey && SqlColumnEnum.INT == dataType && autoIncrement) {
            sql += BaseConst.BLANK + "AUTO_INCREMENT";
        }
        defaultValue = convertDefaultValue(defaultValue, dataType);
        if (!notNull && Objects.isNull(defaultValue)) {
            sql += BaseConst.BLANK + "DEFAULT NULL";
        } else if (notNull) {
            // 没有 default
        } else if (ObjectUtils.isEmpty(defaultValue)) {
            sql += BaseConst.BLANK + "DEFAULT NULL";
        } else {
            sql += BaseConst.BLANK + "DEFAULT " + defaultValue;
        }
        if (!ObjectUtils.isEmpty(remark)) {
            sql += BaseConst.BLANK + "COMMENT '" + remark + "'";
        }
        return sql;
    }

    /**
     * 根据类型转换 defaultValue 的值
     *
     * @param defaultValue 字段默认值
     * @param dataType     字段类型
     * @return Object
     */
    private static Object convertDefaultValue(Object defaultValue, SqlColumnEnum dataType) {
        if (SqlColumnEnum.INT == dataType) {
            if (ObjectUtils.isEmpty(defaultValue)) {
                return 0;
            } else {
                return Integer.valueOf((String) defaultValue);
            }
        }
        return defaultValue;
    }
}
